﻿@inject IDataConfigService DataConfigService
@inject IDialogService DialogService

<MudCard>
    <MudCardContent>
        <SearchFormPartial Model="_searchRequestDto"></SearchFormPartial>
        <SearchTablePartial @ref="_requestTable" ServerData="ServerReload"
                            SearchPartial="_searchPartial"
                            AddHandler="AddHandler">
            <TemplateColumnContent>
                <MudButton StartIcon="@Icons.Material.Filled.Subject" Size="Size.Small"
                           OnClick="@(()=>ManageHandler(context.ConfigCode))">综合管理</MudButton>
            </TemplateColumnContent>
        </SearchTablePartial>
    </MudCardContent>
</MudCard>

@code {

    private DataConfigSearchRequestDto _searchRequestDto = new();
    private IEnumerable<DataConfigInfoResponseDto> _pagedData = new List<DataConfigInfoResponseDto>();
    private SearchPartialInfo _searchPartial = new();
    private SearchTablePartial<DataConfigInfoResponseDto> _requestTable = new();
    private int _totalItems;

    private DialogOptions _dialogOptions = new()
        {
            MaxWidth = MaxWidth.Medium,
            CloseButton = true,
            FullWidth = true,
            DisableBackdropClick = true
        };

    [CascadingParameter]
    public DataConfigIndex? DataConfigIndex { get; set; }

    void ManageHandler(string configCode)
    {
        DataConfigIndex?.SetDataConfigCode(configCode);
        DataConfigIndex?.SetDynamicComponentType(typeof(DataConfigManage));
    }

    void DesignHandle()
    {
        DataConfigIndex?.SetDataConfigCode(string.Empty);
        DataConfigIndex?.SetDynamicComponentType(typeof(DataConfigDesign));
    }

    async Task SearchButtonClick()
    {
        await _requestTable.ReloadServerData();
    }

    async Task AddHandler()
    {
        var dialog = await DialogService.ShowAsync<DataConfigEdit>("新增配置", _dialogOptions);
        var result = await dialog.Result;
        if (!result.Canceled)
        {
            await _requestTable.ReloadServerData();
        }
    }

    async Task EditHandler(DataConfigInfoResponseDto model)
    {
        var parameters = new DialogParameters { { "Key", model.Key } };
        var dialog = await DialogService.ShowAsync<DataConfigEdit>("修改配置", parameters, _dialogOptions);
        var result = await dialog.Result;
        if (!result.Canceled)
        {
            await _requestTable.ReloadServerData();
        }
    }

    /// <summary>
    /// Here we simulate getting the paged, filtered and ordered data from the server
    /// </summary>
    private async Task<TableData<DataConfigInfoResponseDto>> ServerReload(TableState state)
    {
        _searchRequestDto.Page = state.Page + 1;
        _searchRequestDto.Limit = state.PageSize;
        var data = await DataConfigService.QueryDataConfigsByPage(_searchRequestDto);

        _pagedData = data.Data;
        _totalItems = data.TotalCount;

        return new TableData<DataConfigInfoResponseDto>() { TotalItems = _totalItems, Items = _pagedData };
    }

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();

        _searchPartial.IsAdd = true;
        _searchPartial.IsEdit = true;
    }

}